package com.shoppingmall.controller.portal;

import com.shoppingmall.common.ServletResponse;
import com.shoppingmall.pojo.Shipping;
import com.shoppingmall.pojo.User;
import com.shoppingmall.service.ShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created by
 *  管理收货人地址的
 * @author touni
 * @date 2019/3/22
 * @time 10:08
 */
@RestController
@RequestMapping("/shipping")
public class ShippingController {
    @Autowired
    private ShippingService shippingService;

    /**
     * 存储用户的session的名称
     */
    @Value("${USER_SESSION}")
    private String userSessoin;


    /**
     * 添加收货人的信息   或者更新收货人的信息
     * @param session
     * @param shipping
     * @return
     */
    @RequestMapping(value = {"/add","/update"})
    public ServletResponse addShippingAddress(HttpSession session,Shipping shipping){
        //从session中获取user
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage("您还未登录，请先登录");

        }
        //验参
        if (null == shipping || null == shipping.getReceiverAddress()){
            return ServletResponse.createByErrorMessage("参数不合法！！");
        }
        //设置用户id
        shipping.setUserId(userFromSession.getId());
        return shippingService.addOrUpdateShippingAddress(shipping);
    }

    /**
     *
     * 删除收货地址  根据用户id和shippingid
     * @param session
     * @param shippingId
     * @return
     */
    @RequestMapping("/del")
    public ServletResponse deleteShippingAddress(HttpSession session,Integer shippingId){
        //从session中获取user
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage("您还未登录 请先登录");

        }
        //验参
        if (null == shippingId){
            return ServletResponse.createByErrorMessage("参数不合法！！");
        }
        // 为了避免删除到其他用户的地址， 设置成根据用户id和shippingid来删除
        return shippingService.deleteShippingAddress(userFromSession.getId(),shippingId);


    }

    /**
     * 选中查看收货人的具体的地址   防止越权，根据用户id和shippingid一起查询
     * @param session
     * @param shippingId
     * @return
     */
    @RequestMapping("/select")
    public ServletResponse selectShippingAddress(HttpSession session,Integer shippingId){
        //从session中获取user
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage("您还未登录  请先登录");

        }
        //验参
        if (null == shippingId){
            return ServletResponse.createByErrorMessage("参数不合法！！");
        }
        return shippingService.selectShippingAddress(userFromSession.getId(),shippingId);

    }



    @RequestMapping("/list")
    public ServletResponse listShippingAddress(HttpSession session,
                                               @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                               @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        //从session中获取user
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage("您还未登录  请先登录");

        }
        return shippingService.listShippingAddress(userFromSession.getId(),pageNum,pageSize);




    }











}
